﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class ChiTietTuaDeTacGiaDAO
    {
        #region 1. Inserting
        public static bool ThemChiTietTDTG(ChiTietTuaDeTacGiaDTO chitiet)
        {
            bool result = false;
            try
            {
                // Tao list sql parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTuaDe", chitiet.MaTuaDe));
                sqlParams.Add(new SqlParameter("@MaTG", chitiet.MaTG));
                sqlParams.Add(new SqlParameter("@MaCTTD_TG", chitiet.MaChiTietTD_TG));

                // Goi stored procedure
                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertCTTD_TG", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
#region 2. Updating
        public static bool SuaChiTietTDTG(ChiTietTuaDeTacGiaDTO chitiet){
            bool result = false;
            try
            {
                // Tao list sql parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTuaDe", chitiet.MaTuaDe));
                sqlParams.Add(new SqlParameter("@MaTG", chitiet.MaTG));
                sqlParams.Add(new SqlParameter("@MaCTTD_TG", chitiet.MaChiTietTD_TG));

                // Goi stored procedure
                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_UpdateCTTD_TG", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
#endregion
        #region 3. Deleting
        public static bool XoaChiTietTDTG(int machitiet)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaCTTD_TG", machitiet));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeleteCTTD_TG", sqlParams);
                if (n == 1)
                {
                    result = true;
                }

            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static bool XoaChiTietTDTGByTacGia(int matacgia)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTG", matacgia));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeleteCTTD_TGsByAndMaTG", sqlParams);
                if (n >= 1)
                {
                    result = true;
                }

            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static bool XoaChiTietTDTGByTuaDe(String matuade)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTuaDe", matuade));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeleteCTTD_TGsByAndMaTuaDe", sqlParams);
                if (n >= 1)
                {
                    result = true;
                }

            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 4. Retrieving
        public static ChiTietTuaDeTacGiaDTO TimChiTietTDTG(int machitiet)
        {
            ChiTietTuaDeTacGiaDTO result = new ChiTietTuaDeTacGiaDTO();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaCTTD_TG", machitiet));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectCTTD_TG", sqlParams);
                DataRow dr = dt.Rows[0];

                result.MaChiTietTD_TG = int.Parse(dr["MaCTTD_TG"].ToString());
                result.MaTG = int.Parse(dr["MaTG"].ToString());
                result.MaTuaDe = dr["MaTuaDe"].ToString();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static List<ChiTietTuaDeTacGiaDTO> TimChiTietTDTGByTacGia(int matacgia)
        {
            List<ChiTietTuaDeTacGiaDTO> result = new List<ChiTietTuaDeTacGiaDTO>();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTG", matacgia));

                // goi stored procedure
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectCTTD_TGsByAndMaTG", sqlParams);

                foreach (DataRow dr in dt.Rows)
                {
                    ChiTietTuaDeTacGiaDTO tmp = new ChiTietTuaDeTacGiaDTO();
                    tmp.MaChiTietTD_TG = int.Parse(dr["MaCTTD_TG"].ToString());
                    tmp.MaTG = int.Parse(dr["MaTG"].ToString());
                    tmp.MaTuaDe = dr["MaTuaDe"].ToString();

                    result.Add(tmp);
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static List<ChiTietTuaDeTacGiaDTO> TimChiTietTDTGByTuaDeSach(String matuade)
        {
            List<ChiTietTuaDeTacGiaDTO> result = new List<ChiTietTuaDeTacGiaDTO>();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTuaDe", matuade));

                // goi stored procedure
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectCTTD_TGsByAndMaTuaDe", sqlParams);

                foreach (DataRow dr in dt.Rows)
                {
                    ChiTietTuaDeTacGiaDTO tmp = new ChiTietTuaDeTacGiaDTO();
                    tmp.MaChiTietTD_TG = int.Parse(dr["MaCTTD_TG"].ToString());
                    tmp.MaTG = int.Parse(dr["MaTG"].ToString());
                    tmp.MaTuaDe = dr["MaTuaDe"].ToString();

                    result.Add(tmp);
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static bool CheckChiTietTDTGByID(int machitiet)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaCTTD_TG", machitiet));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectCTTD_TG", sqlParams);
                if (dt.Rows.Count > 0)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static DataTable TimTatCaCTTDTG()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectCTTD_TGsAll");
            }
            catch (Exception)
            {
                               
            }
            return dt;
        }

        #endregion
    }
}
